'use strict';

// constants 与 actions 在一起
import { ajax, nameSpace} from 'utils/index';

let ns = nameSpace('HOME');
export const GET_PAGES = ns('GET_PAGES');
export const REMOVE_PAGE = ns('REMOVE_PAGE');
export const LOAD_START  = ns('LOAD_START');
export const LOAD_ERR  = ns('LOAD_ERR');
export const CLOSE_DIALOG = ns('CLOSE_DIALOG');
export const PUBLISH_PAGE = ns('PUBLISH_PAGE');
export const UN_PUBLISH_PAGE = ns('UN_PUBLISH_PAGE');
export const PAGE_PUB_ERROR = ns('PAGE_PUB_ERROR');
export const EDIT_PAGE_TITLE = ns('EDIT_PAGE_TITLE');

export function closeSystemDialog(isShow) {
	return {
    type: CLOSE_DIALOG
  };
}
export function removePage(params) {
  	return (dispatch) => {
		//发送请求前
		dispatch({
		  type: LOAD_START
		});
		ajax({
		  api: 'removePage',
		  method: 'GET',
		  data:params
		},(json) => {
			  //成功后
			  dispatch({
				  type:REMOVE_PAGE,
				  page_id:json.page_id
			  });
		},(json) => {
			  //失败后
			  //(json.code==53) W2超时
			  const login = (json.code==210);
			  dispatch({
				type: LOAD_ERR,
				message:json.msg+(json.sub_msg?json.sub_msg:""),
				loginFalse:login
			  });
		});
	}
}
export function publishPage(params) {
  	return (dispatch) => {
		//发送请求前
		dispatch({
		  type: LOAD_START
		});
		ajax({
		  api: 'publishPage',
		  method: 'GET',
		  data:params
		},(json) => {
			  //成功后
			  dispatch({
				  type:PUBLISH_PAGE,
				  page_id:json.page_id
			  });
		},(json) => {
			  //失败后
			  //(json.code==53) W2超时
			  const login = (json.code==210);
			  dispatch({
				type: LOAD_ERR,
				message:json.msg+(json.sub_msg?json.sub_msg:""),
				loginFalse:login
			  });
		});
	}
}
export function editPageTitle(params) {
  	return (dispatch) => {
		//发送请求前
		dispatch({
		  type: LOAD_START
		});
		ajax({
		  api: 'editPageTitle',
		  method: 'GET',
		  data:params
		},(json) => {
			  //成功后
			  dispatch({
				  type:EDIT_PAGE_TITLE,
				  page_id:json.page_id,
				  name:params.name,
				  preview:params.preview,
			  });
		},(json) => {
			  //失败后
			  //(json.code==53) W2超时
			  const login = (json.code==210);
			  dispatch({
				type: LOAD_ERR,
				message:json.msg+(json.sub_msg?json.sub_msg:""),
				loginFalse:login
			  });
		});
	}
}
export function unPubPage(params) {
  	return (dispatch) => {
		//发送请求前
		dispatch({
		  type: LOAD_START
		});
		ajax({
		  api: 'unPublishPage',
		  method: 'GET',
		  data:params
		},(json) => {
			  //成功后
			  dispatch({
				  type:UN_PUBLISH_PAGE,
				  page_id:json.page_id
			  });
		},(json) => {
			  //失败后
			  //(json.code==53) W2超时
			  const login = (json.code==210);
			  dispatch({
				type: LOAD_ERR,
				message:json.msg+(json.sub_msg?json.sub_msg:""),
				loginFalse:login
			  });
		});
	}
}
export function getPagePubError(params) {
  	return (dispatch) => {
		//发送请求前
		dispatch({
		  type: LOAD_START
		});
		ajax({
		  api: 'getPagePubError',
		  method: 'GET',
		  data:params
		},(json) => {
			  //成功后
			  dispatch({
				type: PAGE_PUB_ERROR,
				msg_list:json.msg
			  });
		},(json) => {
			  //失败后
			  //(json.code==53) W2超时
			  const login = (json.code==210);
			  dispatch({
				type: LOAD_ERR,
				message:json.msg+(json.sub_msg?json.sub_msg:""),
				loginFalse:login
			  });
		});
	}
}
export function getPages(params) {
  	return (dispatch) => {
		//发送请求前
		dispatch({
		  type: LOAD_START
		});
		ajax({
		  api: 'getPages',
		  method: 'GET',
		  data:params
		},(json) => {
			  //成功后
			  dispatch({
				  type:GET_PAGES,
				  pages:json.pages,
				  total:json.total
			  });
		},(json) => {
			  //失败后
			  //(json.code==53) W2超时
			  const login = (json.code==210);
			  dispatch({
				type: LOAD_ERR,
				message:json.msg+(json.sub_msg?json.sub_msg:""),
				loginFalse:login
			  });
		});
	}
}

export function saveAsModel(params) {
  	return (dispatch) => {
		//发送请求前
		dispatch({
		  type: LOAD_START
		});
		ajax({
		  api: 'saveAsModel',
		  method: 'GET',
		  data:params
		},(json) => {
			  //成功后
			  dispatch({
				  type:LOAD_ERR,
				  message:json.msg+(json.sub_msg?json.sub_msg:""),
				  loginFalse:false
			  });
		},(json) => {
			  //失败后
			  //(json.code==53) W2超时
			  const login = (json.code==210);
			  dispatch({
				type: LOAD_ERR,
				message:json.msg+(json.sub_msg?json.sub_msg:""),
				loginFalse:login
			  });
		});
	}
}

